package com.kuai.common.utils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class MapKuaiTools {
	
	@SuppressWarnings("rawtypes")
	public static Map objectToMap(Object obj) {
		Map<String, Object> map = new HashMap<String,Object>();
		log.info(obj.getClass().getTypeName());
		if(obj instanceof ArrayList) {
			map = a(obj);
		    return map;
		}
		if (obj instanceof Object) {
			a(obj);
		}
		return map;
	}
	
	public static Map<String, Object> a(Object obj){
		Map<String, Object> map = new HashMap<String,Object>();
		Class<?> clazz = obj.getClass();
        for (Field field : clazz.getDeclaredFields()) {
            field.setAccessible(true);
            String fieldName = field.getName();
            Object value = null;
			try {
				value = field.get(obj);
				objectToMap(value);
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
            map.put(fieldName, value);
        }
		return map;
	}
}
